package com.bbs.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBHelp<E> {
	private final String DRIVER = "com.mysql.jdbc.Driver";
	private final String URL = "jdbc:mysql:///db_bbs";
	private final String NAME = "root";
	private final String PASSWORD = "root";
	
	/**
	 * 获取数据库连接
	 * @return
	 */
	public Connection getConnection(){
		Connection con = null;
		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(URL,NAME,PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 更新数据
	 * @param sql
	 * @param objects
	 * @return
	 */
	public int update(String sql, Object...objects){
		int row = 0;
		Connection con = null;
		PreparedStatement ps = null;
		
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				ps.setObject(i+1, objects[i]);
			}
			row = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(ps, con);
		}
		return row;
	}
	
	/**
	 * 返回计数
	 * @param sql
	 * @param objects
	 * @return
	 */
	public int queryForCount(String sql, Object...objects ){
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int num = 0;
		
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				ps.setObject(i+1, objects[i]);
			}
			rs = ps.executeQuery();
			if(rs.next()){
				num = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(rs, ps, con);
		}
		return num;
	}
	
	/**
	 * 返回list
	 * @param sql
	 * @param rm
	 * @param objects
	 * @return
	 */
	public List<E> queryForList(String sql ,RowMapper<E> rm , Object...objects){
		List<E> list = new ArrayList<E>();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				ps.setObject(i+1, objects[i]);
			}
			rs = ps.executeQuery();
			while(rs.next()){
				E e = rm.maprow(rs);
				list.add(e);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(rs, ps, con);
		}
		return list;
	}
	
	/**
	 * 返回对象
	 * @param sql
	 * @param rm
	 * @param objects
	 * @return
	 */
	public E queryForObject(String sql ,RowMapper<E> rm , Object...objects){
		E obj = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				ps.setObject(i+1, objects[i]);
			}
			rs = ps.executeQuery();
			if(rs.next()){
				obj = rm.maprow(rs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.close(rs, ps, con);
		}
		return obj;
	}
	
	/**
	 * 数据库连接关闭
	 * @param rs
	 * @param ps
	 * @param con
	 */
	public void close(ResultSet rs ,PreparedStatement ps,Connection con){
		try {
			if(rs != null){
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(ps != null){
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if(con != null){
						con.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void close(PreparedStatement ps,Connection con){
		this.close(null, ps, con);
	}
}
